package dut.ir.lxb.weibo.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dut.ir.lxb.weibo.bean.UserRelationBean;

public class UserRelationDao
{
	private static UserRelationDao ud;
	
	protected String tableName = "T_UserRelation";
	
	private UserRelationDao()
	{
		
	}
	
	public static UserRelationDao getInstance()
	{
		if(ud == null)
		{
			ud = new UserRelationDao();
			return ud;
		}
		else
			return ud;
	}
	
	public boolean hasRecord(UserRelationBean urb)
	{
		if(urb == null)
			return true;
		UserRelationBean checkWb = getUserRelation(urb.getUid1(), urb.getUid2());
		if(checkWb == null)
			return false;
		return true;
	}
	
	public UserRelationBean getUserRelation(String uid1, String uid2)
	{
		DBConnectionManager connectionMan = DBConnectionManager.getInstance();
		Connection conn = null;
		try
		{
			conn = connectionMan.getConnection();
			String sqlStr = "select * from %s where uid1=? and uid2=?";
			sqlStr = String.format(sqlStr, tableName);
			PreparedStatement ps = conn.prepareStatement(sqlStr);
			
			//填充参数
			ps.setString(1, uid1);
			ps.setString(2, uid2);
			
			ResultSet rs = ps.executeQuery();
			UserRelationBean urb = new UserRelationBean();
			if(rs.next())
			{
				
				urb.setUid1(rs.getString(1));
				urb.setUid2(rs.getString(2));
				urb.setComment(rs.getString(3));
				
				
			}
			rs.close();
			ps.close();
			return urb;
			
			
		}
		catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
		finally
		{
			connectionMan.freeConnection(conn);
		}
		
	}
	
	/**
	 * 获得关注列表
	 * @param uid
	 * @return
	 */
	public List<String> getFollowList(String uid)
	{
		DBConnectionManager connectionMan = DBConnectionManager.getInstance();
		Connection conn = null;
		try
		{
			conn = connectionMan.getConnection();
			String sqlStr = "select uid2 from %s where uid1=?";
			sqlStr = String.format(sqlStr, tableName);
			PreparedStatement ps = conn.prepareStatement(sqlStr);
			
			//填充参数
			ps.setString(1, uid);
			
			List<String> followList = new ArrayList<String>();
			ResultSet rs = ps.executeQuery();
			while(rs.next())
			{
				followList.add(rs.getString(1));
			}
			
			rs.close();
			ps.close();
			return followList;
			
			
		}
		catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
		finally
		{
			connectionMan.freeConnection(conn);
		}
	}
	
	
	/**
	 * 获得粉丝列表
	 * @param uid
	 * @return
	 */
	public List<String> getFansList(String uid)
	{
		DBConnectionManager connectionMan = DBConnectionManager.getInstance();
		Connection conn = null;
		try
		{
			conn = connectionMan.getConnection();
			String sqlStr = "select uid1 from %s where uid2=?";
			sqlStr = String.format(sqlStr, tableName);
			PreparedStatement ps = conn.prepareStatement(sqlStr);
			
			//填充参数
			ps.setString(1, uid);
			
			List<String> followList = new ArrayList<String>();
			ResultSet rs = ps.executeQuery();
			while(rs.next())
			{
				followList.add(rs.getString(1));
			}
			
			rs.close();
			ps.close();
			
			return followList;
		}
		catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
		finally
		{
			connectionMan.freeConnection(conn);
		}
	}
	
	public boolean insertUserRelation(UserRelationBean urb)
	{
		DBConnectionManager connectionMan = DBConnectionManager.getInstance();
		Connection conn = null;
		try
		{
			conn = connectionMan.getConnection();
			String sqlStr = "insert into %s(uid1, uid2, comment) values(?,?,?);";
			sqlStr = String.format(sqlStr, tableName);
			PreparedStatement ps = conn.prepareStatement(sqlStr);
			
			//填充参数
			ps.setString(1, urb.getUid1());
			ps.setString(2, urb.getUid2());
			ps.setString(3, urb.getComment());
			
			boolean flag = ps.execute();
			ps.close();
			return flag;
			
		}
		catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		finally
		{
			connectionMan.freeConnection(conn);
		}
	}
	
	public boolean deleteUserRelation(String uid1, String uid2)
	{
		DBConnectionManager connectionMan = DBConnectionManager.getInstance();
		Connection conn = null;
		try
		{
			conn = connectionMan.getConnection();
			String sqlStr = "delete from %s where uid1=? and uid2=?;";
			sqlStr = String.format(sqlStr, tableName);
			PreparedStatement ps = conn.prepareStatement(sqlStr);
			
			//填充参数
			ps.setString(1, uid1);
			ps.setString(2, uid2);
			
			boolean flag = ps.execute();
			ps.close();
			return flag;
			
		}
		catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		finally
		{
			connectionMan.freeConnection(conn);
		}
		
	}
	
	
	
	
}
